package com.be.beadmin.device.controller;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.input.InspectPlanDto;
import com.be.beadmin.device.dto.input.InspectPlanNewDto;
import com.be.beadmin.device.dto.output.EhsItemEx;
import com.be.beadmin.device.entity.EhsItem;
import com.be.beadmin.device.service.EhsStandardService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.EhsItemService;
import com.be.beadmin.device.dto.input.EhsItemDto;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;

/**
* @author zty
* @date 2023-08-13
**/
@RestController
public class EhsItemController {

    @Autowired
    private  EhsItemService ehsItemService;


    @PostMapping(value = "/device/getEhsItemByPage")
    public ResultModel getEhsItemByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(ehsItemService.getEhsItemByPage(pageEntity)) ;
    }

    @ApiOperation("新增ehs巡检项目")
    @PostMapping("/device/addEhsItem")
    public ResultModel addEhsItem(@RequestBody EhsItemDto ehsItemDto){
        ehsItemService.addEhsItem(ehsItemDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delEhsItem")
    public ResultModel delEhsItem(@RequestBody EhsItemDto ehsItemDto){
        ehsItemService.delEhsItem(ehsItemDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateEhsItem")
    public ResultModel updateEhsItem(@RequestBody EhsItemDto ehsItemDto){
        ehsItemService.updateEhsItem(ehsItemDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportEhsItem")
    public void exportEhsItem(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<EhsItemEx> ehsItemPage = ehsItemService.getEhsItemByPage(pageEntity);
        List<EhsItemEx> ehsItemExes = ehsItemPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("EhsItem接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), EhsItemEx.class).sheet("sheet1").doWrite(ehsItemExes);
    }

    @ApiOperation("导入模板")
    @PostMapping("/device/receiveEhsItem")
    public ResultModel receiveEhsItem(@RequestParam String templateId){
        return ResultModel.success(ehsItemService.receiveEhsItem(templateId));
    }

}